#include <QAbstractButton>
#include <QDialogButtonBox>
#include <QPushButton>

#include "creplicationform.h"
#include "ui_creplicationform.h"

CReplicationForm::CReplicationForm(QWidget *parent) :
    QDialog(parent),
    m_ui(new Ui::CReplicationForm), m_currentButton(Synchronize)
{
    m_ui->setupUi(this);

    m_buttons.insert(this->Synchronize, m_ui->syncBtn);
    m_buttons.insert(this->Fetch, m_ui->fetchBtn);
    m_buttons.insert(this->Upload, m_ui->uploadBtn);
    m_buttons.insert(this->Replace, m_ui->replaceBtn);

    foreach(QRadioButton *btn, m_buttons.values())
        connect(btn, SIGNAL(toggled(bool)), this, SLOT(buttonToggled()));

    connect(m_ui->loginEdit, SIGNAL(textChanged(QString)), this, SLOT(checkFields()));
    connect(m_ui->passEdit, SIGNAL(textChanged(QString)), this, SLOT(checkFields()));

    m_ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
}

CReplicationForm::~CReplicationForm()
{
    delete m_ui;
}

void CReplicationForm::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

QString CReplicationForm::actionById(int id)
{
    switch(id)
    {
        case Synchronize: return "sync";
        case Fetch: return "fetch";
        case Upload: return "upload";
        case Replace: return "replace";
    }

    return QString();
}

void CReplicationForm::buttonToggled()
{
    foreach(int btn, m_buttons.keys())
    {
        if(m_buttons.value(btn)->isChecked())
        {
            m_currentButton = btn;
            break;
        }
    }   
}

void CReplicationForm::checkFields()
{
    m_ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(
            m_ui->loginEdit->text().length() && m_ui->passEdit->text().length());
}

QString CReplicationForm::login()
{
    return m_ui->loginEdit->text();
}

QString CReplicationForm::password()
{
    return m_ui->passEdit->text();
}

int CReplicationForm::action()
{
    return m_currentButton;
}

